09. Floating versus Double [demonstration]

Floating and Double Floating Point Numbers

In the last section, you saw a table with fundamental C++ data types and their declarations:

data type declaration
integer int
floating point float
double floating point double
character char
boolean bool
valueless void

What exactly is the difference between a float and a double?

Quiz: Floats versus Doubles

Float versus Double

Which of the following are true about floating point versus double floating point variables?

SOLUTION:
  • A float has about 7 digits after the decimal point whereas a double float can have about 15 digits after the decimal point.
  • In general, using doubles lead to fewer rounding errors.
  • Doubles use more memory.

Both data types can represent numbers containing decimals:

float x = 5.79;

and

double x = 5.79;

However, a double can store about twice as many digits as a float. But there is also a cost; a double requires more memory.

As you'll see in the demonstration below, storing more digits can be important especially when calculations require precision.

Demo: Floating versus Double Type Definitions

The following code will show you the difference between a float data type and a double data type. The code assigns the number 11.0 to a float variable as well as a double variable. Next, each variable is divided by one-hundred thousand.

A for loop then sums each variable one-hundred thousand times to see how close each variable can get to 11.0. You will see that neither variable gets back to exactly 11.0 because memory can only hold a finite number of decimal places. But, the double variable gets closer to exactly 11.0.

Read through the code and then hit the "Test Run" button to see the output of the demonstration.

Start Quiz:

#include <stdio.h>

int main() {
    
    // define floating point numbers    
    float float_num;
    float float_sum = 0;

    double double_num;
    double double_sum = 0;

    int divisions = 100000;

    // divide the floating point numbers by divisions
    float_num = 11.0/divisions;
    double_num = 11.0/divisions;

    // sum the number by the number of divisions to see how close the results
    // get to 11.0
    for (int i = 0; i < divisions; i++) {
        float_sum = float_sum + float_num;
        double_sum = double_sum + double_num;

    }
    
    printf("Floating point sum: %.15g\n", float_sum);
    printf("Double sum: %.15g\n", double_sum);

    return 0;
}